package com.example.UI;

// ... 其他导入 ...

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.ui.R;

public class SettingActivity extends AppCompatActivity {
    private Switch switchNotification;
    private Switch switchAdminMode;
    private TextView textViewLogout;
    private LinearLayout updatePasswordLayout;
    private LinearLayout addStudentLayout;
    private LinearLayout addCourseLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);

        // 初始化UI控件
        updatePasswordLayout = findViewById(R.id.updatePassword);
        switchNotification = findViewById(R.id.switchNotification);
        switchAdminMode = findViewById(R.id.switchAdminMode);
        textViewLogout = findViewById(R.id.textViewLogout);
        addStudentLayout = findViewById(R.id.addStudent);
        addCourseLayout = findViewById(R.id.addCourse);

        switchNotification.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    Toast.makeText(SettingActivity.this, "通知功能已打开", Toast.LENGTH_SHORT).show();

                }else if (!isChecked){
                    Toast.makeText(SettingActivity.this, "通知功能已关闭", Toast.LENGTH_SHORT).show();

                }
            }
        });

        // 设置修改密码的点击事件
        updatePasswordLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 跳转到UpdatePasswordActivity
                Intent intent = new Intent(SettingActivity.this, UpdatePasswordActivity.class);
                startActivity(intent);
            }
        });

        // 设置管理员模式的开关状态变化监听，并初始设置添加学生和添加课程的可见性
        switchAdminMode.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // 根据开关状态显示或隐藏添加学生和添加课程的布局
                addStudentLayout.setVisibility(isChecked ? View.VISIBLE : View.GONE);
                addCourseLayout.setVisibility(isChecked ? View.VISIBLE : View.GONE);
            }
        });

        // 设置添加学生和添加课程的点击事件
        addStudentLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 跳转到AddStudentActivity
                Intent intent = new Intent(SettingActivity.this, AddStudentActivity.class);
                startActivity(intent);
            }
        });

        addCourseLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 跳转到AddCourseActivity
                Intent intent = new Intent(SettingActivity.this, AddCourseActivity.class);
                startActivity(intent);
            }
        });

        // 设置退出登录按钮的点击事件
        textViewLogout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 处理退出登录的逻辑
                SharedPreferences sharedPreferences = getSharedPreferences("user_session", MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.clear(); // 清除所有数据
                editor.apply();

                Toast.makeText(SettingActivity.this, "您已成功退出登录！", Toast.LENGTH_SHORT).show();

                // 退出当前Activity
                finish();

                // 重新启动LoginActivity
                Intent intent = new Intent(SettingActivity.this, LoginActivity.class);
                startActivity(intent);
            }
        });

        // 初始设置添加学生和添加课程的可见性
        switchAdminMode.setChecked(false); // 设置默认为非管理员模式
        addStudentLayout.setVisibility(View.GONE);
        addCourseLayout.setVisibility(View.GONE);
    }
}